<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('ci_type_form.class.php');
require_once('ci_type_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_dbc.class.php');


class CI_Type_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $htdocs_path;
	private $hidden_inc_path;

	function __construct($system_name, Vars_Manager $vm, DB_Connection $dbc, $object_id_capture = 'obj_id', $htdocs_path, $hidden_inc_path) {
		$this->htdocs_path = tangra_normalize_path($htdocs_path);
		$this->hidden_inc_path = tangra_normalize_path($hidden_inc_path);
		parent::__construct($system_name, $vm, $dbc, $object_id_capture);

		$this->export('htdocs_path', $this->htdocs_path);
		$this->export('hidden_inc_path', $this->hidden_inc_path);
	}


	protected function create_form() {
		$form = new CI_Type_Form();

//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new CI_Type_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		if (!file_exists($this->htdocs_path.$this->form->get_field_value('page_edit_path'))) {
			$this->form->set_field_error('page_edit_path', 'file_not_exist');
			$this->export('page_edit_path', $this->form->get_field_value('page_edit_path'));
			$ret = false;
		}

		if (!file_exists($this->htdocs_path.$this->form->get_field_value('page_manage_path'))) {
			$this->form->set_field_error('page_manage_path', 'file_not_exist');
			$this->export('page_manage_path', $this->form->get_field_value('page_manage_path'));
			$ret = false;
		}

		if (!file_exists($this->htdocs_path.$this->form->get_field_value('page_select_path'))) {
			$this->form->set_field_error('page_select_path', 'file_not_exist');
			$this->export('page_select_path', $this->form->get_field_value('page_select_path'));
			$ret = false;
		}

		if (!file_exists($this->htdocs_path.$this->form->get_field_value('page_delete_path'))) {
			$this->form->set_field_error('page_delete_path', 'file_not_exist');
			$this->export('page_delete_path', $this->form->get_field_value('page_delete_path'));
			$ret = false;
		}

		if (!file_exists($this->htdocs_path.$this->form->get_field_value('page_preview_path'))) {
			$this->form->set_field_error('page_preview_path', 'file_not_exist');
			$this->export('page_preview_path', $this->form->get_field_value('page_preview_path'));
			$ret = false;
		}

		if (!file_exists($this->hidden_inc_path.$this->form->get_field_value('class_path'))) {
			$this->form->set_field_error('class_path', 'file_not_exist');
			$this->export('class_path', $this->form->get_field_value('class_path'));
			$ret = false;
		}

		if (!ereg("[a-zA-Z0-9_]{1,255}", $this->form->get_field_value('class_name'))) {
			$this->form->set_field_error('class_name', 'invalid_class_name');
			$this->export('class_name', $this->form->get_field_value('class_name'));
			$ret = false;
		}

		if (!ereg("^[a-z]{1}[a-z0-9_]{0,254}$", $this->form->get_field_value('hid'))) {
			$this->form->set_field_error('hid', 'invalid_hid');
			$ret = false;
		} else {
			$dbc = $this->get_dbc();
			$object = $this->get_object();
			if (!TCMS_CI_Type_DBC::is_unique_hid($dbc, $object->get_id(), $this->form->get_field_value('hid'))) {
				$this->form->set_field_error('hid', 'duplicate_hid');
				$ret = false;
			}
		}

		return $ret;
	}


	protected function on_bad_submit() {

//		printbr('lajna');
	}


	protected function create_object() {
		$object = new TCMS_CI_Type_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('name', $object->get_name());
		$form->set_field_value('hid', $object->get_hid());
		$form->set_field_value('page_edit_path', $object->get_page_edit_path());
		$form->set_field_value('page_manage_path', $object->get_page_manage_path());
		$form->set_field_value('page_select_path', $object->get_page_select_path());
		$form->set_field_value('page_delete_path', $object->get_page_delete_path());
		$form->set_field_value('page_preview_path', $object->get_page_preview_path());
		$form->set_field_value('class_path', $object->get_class_path());
		$form->set_field_value('class_name', $object->get_class_name());
		$form->set_field_value('capture_prefix', $object->get_capture_prefix());
		$form->set_field_value('db_table', $object->get_db_table());
		$form->set_field_value('allow_images', $object->get_allow_images());
		$form->set_field_value('allow_attachments', $object->get_allow_attachments());

	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_name($form->get_field_value('name'));
		$object->set_hid($form->get_field_value('hid'));
		$object->set_page_edit_path($form->get_field_value('page_edit_path'));
		$object->set_page_manage_path($form->get_field_value('page_manage_path'));
		$object->set_page_select_path($form->get_field_value('page_select_path'));
		$object->set_page_delete_path($form->get_field_value('page_delete_path'));
		$object->set_page_preview_path($form->get_field_value('page_preview_path'));
		$object->set_class_path($form->get_field_value('class_path'));
		$object->set_class_name($form->get_field_value('class_name'));
		$object->set_capture_prefix($form->get_field_value('capture_prefix'));
		$object->set_db_table($form->get_field_value('db_table'));
		$object->set_allow_images($form->get_field_value('allow_images'));
		$object->set_allow_attachments($form->get_field_value('allow_attachments'));
	}
}